﻿using System;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Se.Framework.Common;


namespace Se.Framework.Web.Mvc
{
    public class ObjectRegistryControllerFactory : DefaultControllerFactory
    {
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            if (ObjectRegistry.Current.IsRegistered<IController>(controllerName.ToLower()))
            {
                return ObjectRegistry.Current.Resolve<IController>(controllerName.ToLower());
            }

            return base.CreateController(requestContext, controllerName);
        }

        public override void ReleaseController(IController controller)
        {
            ObjectRegistry.Current.Clean(controller);
            base.ReleaseController(controller);
        }
    }
}
